package com.example.server.handler;

import com.example.message.ChatRequestMessage;
import com.example.message.ChatResponseMessage;
import com.example.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

// 只关心ChatRequestMessage消息
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage message) throws Exception {
        String to = message.getTo();
        String from = message.getFrom();
        // 获取到对方的channel
        Channel channel = SessionFactory.getSession().getChannel(to);
        if (channel != null) {
            // 在线
            channel.writeAndFlush(new ChatResponseMessage(message.getFrom(), message.getContent()));
        } else {
            // 不在线
            ctx.channel().writeAndFlush(new ChatResponseMessage(false, "对方不在线。"));
        }
    }
}
